package com.ruige.account.controller;


import com.ruige.account.entity.Result;
import com.ruige.account.entity.StatusCode;
import com.ruige.account.pojo.AccountLog;
import com.ruige.account.service.IAccountLogService;
import com.ruige.account.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Transactional(rollbackFor = {Exception.class})
@RequestMapping("/account")
public class AccountController {
    @Autowired
    private IAccountService accountService;
    @Autowired
    private IAccountLogService accountLogService;


    /**
     * 记账
     *
     * @param accountLog
     * @param
     * @return
     */
    @PostMapping("/change")
    public Result change(@RequestBody AccountLog accountLog) {
        if (accountLog.getChangeMoney() != null && accountLog.getChangeType() != null && accountLog.getUserId() != null) {
            accountLogService.change(accountLog);
            return new Result(true, StatusCode.OK, "记账成功!");
        } else {
            return new Result(false, StatusCode.ERROR, "记账失败!");
        }
    }


    /**
     * 修改
     *
     * @param accountLog
     * @return
     */
    @PostMapping("/editLog")
    public Result editLog(@RequestBody AccountLog accountLog) {
        if (accountLog != null && accountLog.getChangeMoney() != null && accountLog.getChangeType() != null && accountLog.getUserId() != null) {
            accountLogService.editLog(accountLog);
            return new Result(true, StatusCode.OK, "修改账单成功!");
        } else {
            return new Result(false, StatusCode.ERROR, "修改账单失败!");
        }
    }
}
